package com.beaven.model.model.db;

import com.beaven.model.model.sp.Account;
import com.beaven.model.utils.DataUtil;
import com.raizlabs.android.dbflow.annotation.Column;
import com.raizlabs.android.dbflow.annotation.ForeignKey;
import com.raizlabs.android.dbflow.annotation.PrimaryKey;
import com.raizlabs.android.dbflow.annotation.Table;
import java.io.Serializable;
import java.util.Date;

/**
 * author: Beaven
 * time: 2017/5/18 14:39
 */
@Table(database = AppDatabase.class) public class Message extends BaseDbModel<Message>
    implements Serializable {

  // 接收者类型
  public static final int RECEIVER_TYPE_NONE = 1;
  public static final int RECEIVER_TYPE_GROUP = 2;

  // 消息类型
  public static final int TYPE_STR = 1;

  // 消息状态
  public static final int STATUS_DONE = 0; // 正常状态
  public static final int STATUS_CREATED = 1; // 创建状态
  public static final int STATUS_FAILED = 2; // 发送失败状态

  @PrimaryKey private String id;

  // 消息内容
  @Column private String content;

  @Column private int type;

  @Column private Date createAt;

  // 当前消息的状态
  @Column private int status;

  @ForeignKey(tableClass = User.class, stubbedRelationship = true) private User sender;

  @ForeignKey(tableClass = User.class, stubbedRelationship = true) private User receiver;

  @ForeignKey(tableClass = Group.class, stubbedRelationship = true) private Group receiveGroup;

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getContent() {
    return content;
  }

  public void setContent(String content) {
    this.content = content;
  }

  public int getType() {
    return type;
  }

  public void setType(int type) {
    this.type = type;
  }

  public Date getCreateAt() {
    return createAt;
  }

  public void setCreateAt(Date createAt) {
    this.createAt = createAt;
  }

  public int getStatus() {
    return status;
  }

  public void setStatus(int status) {
    this.status = status;
  }

  public User getSender() {
    return sender;
  }

  public void setSender(User sender) {
    this.sender = sender;
  }

  public User getReceiver() {
    return receiver;
  }

  public void setReceiver(User receiver) {
    this.receiver = receiver;
  }

  public Group getReceiveGroup() {
    return receiveGroup;
  }

  public void setReceiveGroup(Group receiveGroup) {
    this.receiveGroup = receiveGroup;
  }

  User getOther() {
    if (Account.getUserId().equals(sender.getId())) {
      return receiver;
    } else {
      return sender;
    }
  }

  @Override public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Message message = (Message) o;

    return type == message.type
        && status == message.status
        && DataUtil.equals(id, message.id)
        && DataUtil.equals(content, message.content)
        && DataUtil.equals(createAt, message.createAt)
        && DataUtil.equals(receiveGroup, message.receiveGroup)
        && DataUtil.equals(sender, message.sender)
        && DataUtil.equals(receiver, message.receiver);
  }

  @Override public int hashCode() {
    return id != null ? id.hashCode() : 0;
  }

  @Override public boolean isSame(Message oldT) {
    // 两个类，是否指向的是同一个消息
    return DataUtil.equals(id, oldT.id);
  }

  @Override public boolean isUiContentSame(Message oldT) {
    // 对于同一个消息当中的字段是否有不同
    // 这里，对于消息，本身消息不可进行修改；只能添加删除
    // 唯一会变化的就是本地（手机端）消息的状态会改变
    return oldT == this || status == oldT.status;
  }
}
